package com.lsfb.signln.Statistics.HistoryRecord;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ajguan.library.EasyRefreshLayout;
import com.lsfb.signln.Login.LoginActivity;
import com.lsfb.signln.R;
import com.lsfb.signln.Utils.LittleUtils;
import com.lsfb.signln.Utils.LsfbUtils;
import com.lsfb.signln.Utils.MyProgressDialog;
import com.lsfb.signln.Utils.PopWindowManager;
import com.lsfb.signln.Utils.URLString;
import com.lsfb.signln.View.SignLoadview;
import com.lsfb.signln.View.SignRefreshHeadView;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 历史记录
 * Created by Administrator on 2018/1/30 0030.
 */
@ContentView(R.layout.aty_history_record)
public class HistoryRecordActivity extends LsfbActivity {

    /**
     * 标题栏
     */
    @ViewInject(R.id.aty_title)
    private RelativeLayout aty_title;

    /**
     * 列表数据
     */
    @ViewInject(R.id.aty_history_record_rv)
    private RecyclerView rv_record;
    private List<HistoryRecordBean.ListBean> datas;
    private HistoryRecordAdapter adapter;

    /**
     * 暂无数据
     */
    @ViewInject(R.id.aty_statistics_leave_layout_null)
    private RelativeLayout layout_null;


    /**
     * 下拉刷新 上拉加载
     */
    @ViewInject(R.id.easylayout)
    private EasyRefreshLayout easylayout;

    private boolean IsRefresh = true;

    /**
     * PopWindow管理类
     */
    private PopWindowManager replay;

    private MyProgressDialog dialog;

    /**
     * 1按日期升序排列2按日期降序排列3按人员名称排列
     */
    private int select_sort = 1;

    /**
     * 0默认1只看外勤打卡2只看手写考勤
     */
    private int select_filtrate = 0;

    //人员类型
    private String usertype = "0";
    //下属人员
    private String users = null;
    //部门
    private String depars = null;
    //时间类型
    private int timetype = 0;
    //开始时间
    private String start_time = "";
    //结束时间
    private String end_time = "";
    // 内勤/外勤
    private String moldType = "";
    // 筛选
    private String orderby = "0";
    private String state = "0";
    //页数
    private int page = 0;

    @Override
    public void initView() {
        if (getIntent().getStringExtra("type") != null) {
            usertype = getIntent().getStringExtra("type");
            timetype = getIntent().getIntExtra("timetype", 0);
            if (timetype == 3) {
                start_time = getIntent().getStringExtra("startTime");
                end_time = getIntent().getStringExtra("endTime");
            }
            moldType = getIntent().getStringExtra("moldType");
        } else {
            T.showShort(this, "数据获取异常");
            finish();
            return;
        }

        dialog = new MyProgressDialog(this, "加载中......");

        easylayout.setLoadMoreView(new SignLoadview(this));
        easylayout.setRefreshHeadView(new SignRefreshHeadView(this));
        easylayout.addEasyEvent(new EasyRefreshLayout.EasyEvent() {
            //加载更多
            @Override
            public void onLoadMore() {
                page++;
                IsRefresh = false;
                getRecord();
            }

            //刷新
            @Override
            public void onRefreshing() {
                page = 0;
                IsRefresh = true;
                getRecord();
            }
        });


    }

    /**
     * 获取内外勤定位详情
     */
    private void getRecord() {
        HashMap hashMap = new HashMap();
        hashMap.put("type", usertype);
        if (usertype.equals("1")) {
            if (getIntent().getStringExtra("person") != null) {
                users = getIntent().getStringExtra("person");
                hashMap.put("persons", users);
            }
        }
        hashMap.put("timetype", String.valueOf(timetype));
        if (timetype == 3) {
            hashMap.put("starTime", start_time);
            hashMap.put("endTime", end_time);
        }
        hashMap.put("page", String.valueOf(page));
        hashMap.put("moldType", moldType);
        if (moldType.equals("1")) {
            hashMap.put("state", state);
        }
        hashMap.put("orderby", orderby);
        dialog.showdialog();
        new BaseInternet().getData(URLString.DETAILSSIGN, hashMap, HistoryRecordBean.class,
                new HistoryRecordEvent<HistoryRecordBean>(), true);
    }

    @EventAnnotation
    public void eventResult(HistoryRecordEvent<HistoryRecordBean> event) {
        dialog.hidedialog();
        easylayout.refreshComplete();
        easylayout.loadMoreComplete();
        switch (event.getCode()) {
            case 200:
                if (event.getData().getList() != null && !event.getData().getList().isEmpty()) {
                    if (IsRefresh) {
                        datas.clear();
                        rv_record.onScrolled(0, 0);
                    }
                    datas.addAll(event.getData().getList());
                    layout_null.setVisibility(View.GONE);
                } else {
                    if (IsRefresh) {
                        datas.clear();
                        layout_null.setVisibility(View.VISIBLE);
                    } else {
                        page--;
                        T.showShort(this, "没有更多数据");
                        layout_null.setVisibility(View.GONE);
                    }
                }
                adapter.notifyDataSetChanged();
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

    @Override
    public void initEvent() {

    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_back, R.id.aty_select})
    public void onClick(View view) {
        switch (view.getId()) {
            //返回
            case R.id.aty_back:
                finish();
                break;
            //选择器
            case R.id.aty_select:
                showPopWindow();
                break;
        }
    }

    @Override
    public void initData() {
        datas = new ArrayList<>();
        adapter = new HistoryRecordAdapter(this, datas, R.layout.item_signln);
        rv_record.setLayoutManager(new LinearLayoutManager(this));
        rv_record.addItemDecoration(LsfbUtils.getVERTICALLineDivider(this, R.color.white_color, 10));
        rv_record.setAdapter(adapter);

        getRecord();
    }

    private void showPopWindow() {
        replay = new PopWindowManager(this, R.layout.pop_statistics_select) {
            @Override
            public void onViewLister(View contentView, final PopWindowManager manager) {

                final LinearLayout layout_gray = (LinearLayout) contentView.findViewById(R.id.layout_gray);
                final LinearLayout pop_out = contentView.findViewById(R.id.pop_out);
                //只看外勤打卡
                final RelativeLayout select_out = (RelativeLayout) contentView.findViewById(R.id.pop_select_out);
                final ImageView select_out_img = (ImageView) contentView.findViewById(R.id.pop_select_out_img);
                //只看手写考勤
                final RelativeLayout select_signature = (RelativeLayout) contentView.findViewById(R.id.pop_select_signature);
                final ImageView select_signature_img = (ImageView) contentView.findViewById(R.id.pop_select_signature_img);

                //升序
                final RelativeLayout select_date_top = (RelativeLayout) contentView.findViewById(R.id.pop_select_date_top);
                final ImageView select_date_top_img = (ImageView) contentView.findViewById(R.id.pop_select_date_top_img);
                //降序
                final RelativeLayout select_date_bottom = (RelativeLayout) contentView.findViewById(R.id.pop_select_date_bottom);
                final ImageView select_date_bottom_img = (ImageView) contentView.findViewById(R.id.pop_select_date_bottom_img);
                //人员名称
                final RelativeLayout select_personnel = (RelativeLayout) contentView.findViewById(R.id.pop_select_personnel);
                final ImageView select_personnel_img = (ImageView) contentView.findViewById(R.id.pop_select_personnel_img);
                //修改
                final TextView select_change = (TextView) contentView.findViewById(R.id.pop_select_change);

                //1按日期升序排列2按日期降序排列3按人员名称排列
                if (orderby.equals("0")) {
                    select_date_top_img.setVisibility(View.VISIBLE);
                    select_date_bottom_img.setVisibility(View.GONE);
                    select_personnel_img.setVisibility(View.GONE);
                } else if (orderby.equals("1")) {
                    select_date_top_img.setVisibility(View.GONE);
                    select_date_bottom_img.setVisibility(View.VISIBLE);
                    select_personnel_img.setVisibility(View.GONE);
                } else if (orderby.equals("2")) {
                    select_date_top_img.setVisibility(View.GONE);
                    select_date_bottom_img.setVisibility(View.GONE);
                    select_personnel_img.setVisibility(View.VISIBLE);
                }

                if (moldType.equals("1")) { //外勤
                    pop_out.setVisibility(View.VISIBLE);

                    //0默认1只看外勤打卡2只看手写考勤
                    if (state.equals("0")) {
                        select_out_img.setImageResource(R.mipmap.img_select);
                        select_signature_img.setImageResource(R.mipmap.img_select);
                    } else if (state.equals("1")) {
                        select_out_img.setImageResource(R.mipmap.img_selected);
                        select_signature_img.setImageResource(R.mipmap.img_select);
                    } else if (state.equals("2")) {
                        select_out_img.setImageResource(R.mipmap.img_select);
                        select_signature_img.setImageResource(R.mipmap.img_selected);
                    }
                } else {
                    pop_out.setVisibility(View.GONE);
                }

                layout_gray.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        manager.hideWindow();
                    }
                });

                //只看外勤打卡
                select_out.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        state = "1";
                        select_out_img.setImageResource(R.mipmap.img_selected);
                        select_signature_img.setImageResource(R.mipmap.img_select);
                    }
                });

                //只看手写考勤
                select_signature.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        state = "2";
                        select_out_img.setImageResource(R.mipmap.img_select);
                        select_signature_img.setImageResource(R.mipmap.img_selected);
                    }
                });

                //按日期升序排列
                select_date_top.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        orderby = "0";
                        select_date_top_img.setVisibility(View.VISIBLE);
                        select_date_bottom_img.setVisibility(View.GONE);
                        select_personnel_img.setVisibility(View.GONE);
                    }
                });

                //按日期降序排列
                select_date_bottom.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        orderby = "1";
                        select_date_top_img.setVisibility(View.GONE);
                        select_date_bottom_img.setVisibility(View.VISIBLE);
                        select_personnel_img.setVisibility(View.GONE);
                    }
                });

                //按人员名称排列
                select_personnel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        orderby = "2";
                        select_date_top_img.setVisibility(View.GONE);
                        select_date_bottom_img.setVisibility(View.GONE);
                        select_personnel_img.setVisibility(View.VISIBLE);
                    }
                });

                //提交
                select_change.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getRecord();
                        manager.hideWindow();
                    }
                });

            }
        };
        replay.showWindowsTop(aty_title);
    }
}
